package com.dt76.smallloanbackenduser.controller.etao;

import com.dt76.smallloanbackenduser.pojo.etao.CurrentUser;
import com.dt76.smallloanbackenduser.pojo.etao.Department;
import com.dt76.smallloanbackenduser.pojo.etao.Employee;
import com.dt76.smallloanbackenduser.remote.etao.PrivilegeManagement;
import com.dt76.smallloanbackenduser.utils.Page;
import com.dt76.smallloanbackenduser.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/dept")
public class DepartmentController {

    @Autowired
    PrivilegeManagement privilegeManagement;

    @RequestMapping("/select")
    public String selectAllDept(Model model,@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum) {
        Page page = privilegeManagement.getAllDept(pageNum);
        model.addAttribute("allDept", page.getRows());
        model.addAttribute("pageInfo", page);
        List<Integer> pageArr = new ArrayList<Integer>();
        for (Integer i = 0; i < page.getTotalPageCount(); i++) {
            pageArr.add(i+1);
        }
        model.addAttribute("pageArr",pageArr);
        return "pages/etao/department";
    }

    @RequestMapping("/selectPage")
    @ResponseBody
    public String selectPage(Integer pageNum) {
        return "/dept/select?pageNum=" + pageNum;
    }


    @RequestMapping("/getOneDept")
    @ResponseBody
    public Department getOneDept(String updDid) {
        Department dept = privilegeManagement.getDeptById(Integer.valueOf(updDid));
        return dept;
    }

    @RequestMapping("/delDept")
    @ResponseBody
    public Result delDept(String delDeptId, HttpSession session) {
        //获取当前操作人
        Employee sessionEmp = (Employee) session.getAttribute("sessionEmp");
        CurrentUser currentUser = new CurrentUser();
        currentUser.setcEmp(sessionEmp);
        Result result = privilegeManagement.delDeptById(Integer.valueOf(delDeptId),currentUser);
        if (result.isSuccess()) {
            result.setMsg("删除成功");
        } else {
            result.setMsg("删除失败");
        }
        return result;
    }


    @RequestMapping("/addDept")
    @ResponseBody
    public Result addDept(HttpSession session,
            @RequestParam("deptName") String deptName,
            String deptId,
            Department department) {
        Employee sessionEmp =(Employee) session.getAttribute("sessionEmp");
        Result result = new Result();
        if (deptId.equals("0")) {//id等于0时走新增
            department.setdName(deptName);
            department.setCreateBy(sessionEmp.geteId());
            result = privilegeManagement.addDept(department,sessionEmp.geteId());
            if (result.isSuccess()) {
                result.setMsg("添加成功");
            } else {
                result.setMsg("添加失败");
            }
        } else {//有id走修改
            department.setDid(Integer.valueOf(deptId));
            department.setdName(deptName);
            department.setModifyBy(sessionEmp.geteId());
            result = privilegeManagement.updDept(department,sessionEmp.geteId());
            if (result.isSuccess()) {
                result.setMsg("修改成功");
            } else {
                result.setMsg("修改失败");
            }
        }
        return result;
    }

    @RequestMapping("/updDeptEnable")
    @ResponseBody
    public Result updDeptEnable(String updEnable, String deptId) {
        Result result = privilegeManagement.updDeptEnable(Integer.valueOf(deptId), Integer.valueOf(updEnable));
        if (result.isSuccess()) {
            result.setMsg("修改成功");
        } else {
            result.setMsg("修改失败");
        }
        return result;
    }

}
